package com.ttctracker.map;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
//import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.ttctracker.classes.PathPoint;
import com.ttctracker.classes.RoutePath;

public class RoutePathOverlay extends Overlay {

	Projection projection;
	ArrayList<RoutePath> paths;
	Bitmap pathCache;
	GeoPoint lastCenter;
	int lastZoomLat, lastZoomLon, zoomLevel;
	protected Timer timer;
	MapView mapView;
	TimerTask task;
	ArrayList<ArrayList<GeoPoint>> pathGeoPoints;

	public RoutePathOverlay(ArrayList<RoutePath> paths) {
		this.paths = paths;
		getGeopoints();
	}

	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		//
		super.draw(canvas, mapv, false);
		Paint mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setColor(Color.RED);
		mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(10);
		mPaint.setAntiAlias(true);
		mPaint.setAlpha(70);
		Path path = new Path();
		projection = mapv.getProjection();

		path = drawPath();
		canvas.drawPath(path, mPaint);
	}

	/*private void panAfterZoom(MapView mv, long delay) {
		timer = new java.util.Timer("drawtimer", true);
		mapView = mv;
		task = new java.util.TimerTask() {
			public void run() {
				GeoPoint center = mapView.getMapCenter();
				GeoPoint point = new GeoPoint(center.getLatitudeE6() + 10,
						center.getLongitudeE6());
				MapController contr = mapView.getController();
				contr.setCenter(point);
				timer.cancel();
			}
		};
		timer.schedule(task, delay);
	}
*/
	private Path drawPath() {
		Path path = new Path();
		int pointslength = 0;

		for (ArrayList<GeoPoint> geoPoints : pathGeoPoints) {
			pointslength = geoPoints.size();
			for (int j = 1; j < pointslength; j++) {

				Point p1 = new Point();
				Point p2 = new Point();

				projection.toPixels(geoPoints.get(j - 1), p1);
				projection.toPixels(geoPoints.get(j), p2);

				path.moveTo(p2.x, p2.y);
				path.lineTo(p1.x, p1.y);

			}
		}

		return path;
	}

	private void getGeopoints() {
		pathGeoPoints = new ArrayList<ArrayList<GeoPoint>>();
		ArrayList<GeoPoint> geoPoints;
		RoutePath routePath;
		int pathslength = paths.size();
		int pointslength = 0;
		ArrayList<PathPoint> points;

		for (int i = 0; i < pathslength; i++) {
			routePath = paths.get(i);
			points = routePath.getPoints();
			pointslength = points.size();
			geoPoints = new ArrayList<GeoPoint>();
			for (int j = 0; j < pointslength; j++) {
				GeoPoint gP = new GeoPoint(
						(int) (points.get(j).get_lat() * 1e6), (int) (points
								.get(j).get_lon() * 1e6));

				geoPoints.add(gP);
			}
			pathGeoPoints.add(geoPoints);
		}
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		// TODO Auto-generated method stub
		return super.draw(canvas, mapView, shadow, when);
	}

}
